package com.bbx.taxi.client.Adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.comm.returns.line.To_Cities;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderList;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialPriceDetail;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.VehicleInfo;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.taxi.official.R;
import com.bbx.taxi.official.R.color;
import com.bbx.taxi.client.Activity.MainActivity;
import com.bbx.taxi.client.Activity.Address.AddAddressActivity;
import com.bbx.taxi.client.Activity.Menu.SetActivity;
import com.bbx.taxi.client.Activity.Order.OrderEvaluationActivity;
import com.bbx.taxi.client.Activity.Order.OrderEvaluationActivity2;
import com.bbx.taxi.client.Activity.Order.StartServiceActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.Bean.Extra.OrderMsg;
import com.bbx.taxi.client.DB.LineDB;
import com.bbx.taxi.client.Util.FormatUtil;
import com.bbx.taxi.client.Util.Tel;
import com.bbx.taxi.client.widget.MyViewPager;
import com.example.Util.DensityUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyOrderAdapter extends BaseAdapter {
	private ArrayList<OfficialOrderList> list;
	private Activity context;
	private boolean isItemClick = false;
	private MyViewPager mViewPager;
	private int requestCode;
	private LineDB db_line;// 线路数据库

	public MyOrderAdapter(Activity context, ArrayList<OfficialOrderList> list,
			MyViewPager mViewPager) {
		this.list = list;
		this.mViewPager = mViewPager;
		this.context = context;
		db_line = new LineDB(context);
	}

	public MyOrderAdapter(Activity context, ArrayList<OfficialOrderList> list) {
		this.list = list;
		this.context = context;
		db_line = new LineDB(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	public boolean getItemClick() {
		return isItemClick;
	}

	public void setItemClick(boolean isItemClick) {
		this.isItemClick = isItemClick;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			convertView = (View) inflater.inflate(R.layout.layout_order_detail,
					null);
			;
			viewholder = new ViewHolder();
			viewholder.iv_own = (ImageView) convertView
					.findViewById(R.id.iv_own);
			viewholder.tv_status = (TextView) convertView
					.findViewById(R.id.tv_status);
			viewholder.tv_time = (TextView) convertView
					.findViewById(R.id.tv_time);
			viewholder.order_type = (TextView) convertView
					.findViewById(R.id.order_type);
			viewholder.tv_start = (TextView) convertView
					.findViewById(R.id.tv_start);
			viewholder.tv_end = (TextView) convertView
					.findViewById(R.id.tv_end);
			viewholder.tv_price = (TextView) convertView
					.findViewById(R.id.tv_price);
			viewholder.ring_up = (ImageView) convertView
					.findViewById(R.id.ring_up);
			viewholder.layout_item = (LinearLayout) convertView
					.findViewById(R.id.layout_item);
			viewholder.layout_price = (LinearLayout) convertView
					.findViewById(R.id.layout_price);
			viewholder.layout_status = (LinearLayout) convertView
					.findViewById(R.id.layout_status);

			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();
        if(list==null||list.size()<=position){
        	return convertView;
        }
		// 判断自费类型
		try {
			if (Integer.parseInt(list.get(position).getOwn_expense()) == Value.NOOWN) {
				viewholder.iv_own.setImageDrawable(context.getResources()
						.getDrawable(R.drawable.icon_g));
			} else {
				viewholder.iv_own.setImageDrawable(context.getResources()
						.getDrawable(R.drawable.icon_z));
			}
		} catch (Exception e) {
		}

		viewholder.layout_price.setVisibility(View.VISIBLE);

		// ///////////////先判断派车的汽车类型，，没有，则判断下单的汽车类型/////////////////////////////
		VehicleInfo mVehicleInfo = null;
		try {
			mVehicleInfo = list.get(position).getVehicleDetail()
					.getVehicleInfo();
		} catch (Exception e) {
		}

		if (mVehicleInfo != null) {
			int paidan_type = mVehicleInfo.getVtype();
			if (paidan_type == Value.taxi_vtype) {
				viewholder.layout_price.setVisibility(View.GONE);
			}
		} else {
			try {
				int type = list.get(position).getVehicleDetail().getVtype();
				if (type == Value.taxi_vtype) {
					viewholder.layout_price.setVisibility(View.GONE);
				}
			} catch (Exception e) {
			}
		}
		// 设置价格的显示跟隐藏
		 setPriceVisible(viewholder, list, position);
		// ////////////////////////已取消显示消单费////////////////////////////////////////////////
		String audit = list.get(position).getAudit_status();
		int status = Integer.parseInt(list.get(position).getOrder_status());
		if (status == Value.ORDER_STATUS_CANCEL
				|| status == Value.ORDER_STATUS_KF_CANCEL
				|| status == Value.ORDER_STATUS_GAIPAI_CANCEL) {
			// 价格
			JSONObject json_price_detail = null;
			try {
				if (list.get(position).getPrice_Detail() != null)
					json_price_detail = new JSONObject(list.get(position)
							.getPrice_Detail());
			} catch (JSONException e) {
				e.printStackTrace();
			}

			if (json_price_detail != null) {
				OfficialPriceDetail price_detail = (OfficialPriceDetail) new JsonBuild()
						.getData(OfficialPriceDetail.class,
								json_price_detail.toString());
				viewholder.tv_price.setText(FormatUtil
						.onFormatPrice(price_detail.getCancelPrice()));
				viewholder.layout_price.setVisibility(View.VISIBLE);
			}else{
				viewholder.tv_price.setText("0");
				viewholder.layout_price.setVisibility(View.VISIBLE);
			}

		}
		

		// ////////////////////////////设置地址跟具体状态/////////////////////////////////////////
		viewholder.tv_time.setText(list.get(position).getAppoint_time());
		// viewholder.order_type.setText(getType(list.get(position).order_type));
		if(list.get(position).getOrderDetail()!=null&&list.get(position).getOrderDetail().start!=null&&list.get(position).getOrderDetail().end!=null){
		viewholder.tv_start
				.setText(list.get(position).getOrderDetail().start.address);
		viewholder.tv_end
				.setText(list.get(position).getOrderDetail().end.address);
		}
		// getPhoneType(viewholder.ring_up, position);
		getStatus(viewholder,
				Integer.parseInt(list.get(position).order_status),
				Integer.parseInt(list.get(position).getValuate_status()), list
						.get(position).getAudit_status());
		viewholder.layout_item.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (mViewPager != null)
					mViewPager.setNoScroll(true);

				return false;
			}
		});
		return convertView;

	}

	public class ViewHolder {
		TextView tv_time, order_type, tv_start, tv_end, tv_status, tv_price;
		ImageView ring_up;// 打电话
		ImageView iv_own;
		LinearLayout layout_item, layout_price;
		LinearLayout layout_status;
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				// context.startActivity(new Intent(context,
				// AddAddressActivity.class));
				break;
			}
		};
	};

	/**
	 * 设置价格的显示跟不显示
	 */
	public void setPriceVisible(final ViewHolder viewholder,
			final ArrayList<OfficialOrderList> list, final int position) {
		String audit = list.get(position).getAudit_status();
		int status = Integer.parseInt(list.get(position).getOrder_status());
		if (audit.equals(Value.STATUS_AUDIT_NOPASS)) {
			viewholder.layout_price.setVisibility(View.GONE);
		} else {
			switch (status) {
			case Value.ORDER_STATUS_UNTREATED:// 未处理
			case Value.ORDER_STATUS_TEAM:// 已入队列
			case Value.ORDER_STATUS_RETEAM:// 重新调度入队列
			case Value.ORDER_STATUS_GAIPAI:// 改派
			case Value.ORDER_STATUS_KF_GAIPAI:// 改派
			case Value.ORDER_STATUS_ASSiIGN:// 已指派
			case Value.ORDER_SIJI_CONFIRM:// 司机确认
				String price = list.get(position).getPrice();
				if (price != null && !price.equals("-1") && !price.equals("")) {
					viewholder.tv_price
							.setText(FormatUtil.onFormatPrice(price));
				} else {
					viewholder.tv_price.setText("0.0");
				}
				break;
			case Value.ORDER_STATUS_ONCAR:// 已上车
			case Value.ORDER_STATUS_APPOINT:// 到达指定地点
				viewholder.layout_price.setVisibility(View.GONE);
				break;
			case Value.ORDER_STATUS_OFFCAR:// 已下车，待支付
			case Value.ORDER_STATUS_FINISH:// 已完成
			case Value.ORDER_STATUS_CANCEL:// 已取消
			case Value.ORDER_STATUS_KF_CANCEL:// 已取消
			case Value.ORDER_STATUS_GAIPAI_CANCEL:// 已取消
				viewholder.layout_price.setVisibility(View.VISIBLE);
				//先去最外层的actual_price，然后再去price_detail里的actual_price
				String actual_price = list.get(position).getActual_price();

				// 设置price_detail里面的价格
				if (list.get(position).getPrice_Detail() != null) {
					OfficialPriceDetail price_detail = null;
					JSONObject json_price_detail = null;
					try {
						json_price_detail = new JSONObject(list.get(position)
								.getPrice_Detail());
						actual_price = json_price_detail
								.getString("actual_price");
					} catch (JSONException e) {
						e.printStackTrace();
					}
					if (actual_price != null && !actual_price.equals("-1")
							&& !actual_price.equals("")) {
						viewholder.tv_price.setText(FormatUtil
								.onFormatPrice(actual_price));
					} else {
						viewholder.tv_price.setText("0.0");
					}
				}
			}
		}
	}

	/**
	 * “order_status”:0:已提交,1:已派单,2:已结束 获取电话类型，，；等待派车，服务中，待评价
	 */
	public void getStatus(ViewHolder viewholder, int order_status,
			int valuate_status, String audit_status) {
		viewholder.layout_status.setVisibility(View.VISIBLE);
		if (order_status == Value.ORDER_STATUS_UNTREATED
				&& audit_status != null
				&& audit_status.equals(Value.STATUS_AUDIT_NO)) {
			viewholder.tv_status.setText("待审核");
			viewholder.layout_status
					.setBackgroundResource(R.drawable.icon_dd_label2);
		} else if (order_status == Value.ORDER_STATUS_UNTREATED
				&& audit_status != null
				&& audit_status.equals(Value.STATUS_AUDIT_NOPASS)) {
			viewholder.tv_status.setText("订单驳回");
			viewholder.layout_status
					.setBackgroundResource(R.drawable.icon_dd_label1);
		} else {
			switch (order_status) {
			case Value.ORDER_STATUS_UNTREATED:// 未处理
			case Value.ORDER_STATUS_TEAM:// 已入队列
			case Value.ORDER_STATUS_RETEAM:// 重新调度入队列
				viewholder.layout_status
						.setBackgroundResource(R.drawable.icon_dd_label3);
				viewholder.tv_status.setText(R.string.order_status_wait);
				break;
			case Value.ORDER_STATUS_GAIPAI:// 改派
			case Value.ORDER_STATUS_KF_GAIPAI:// 改派
				viewholder.layout_status
						.setBackgroundResource(R.drawable.icon_dd_label3);
				viewholder.tv_status.setText(R.string.order_status_wait);
				break;
			case Value.ORDER_STATUS_ASSiIGN:// 已指派
			case Value.ORDER_SIJI_CONFIRM:// 司机确认
			case Value.ORDER_STATUS_ONCAR:// 已上车
			case Value.ORDER_STATUS_APPOINT:// 到达指定地点
				viewholder.tv_status.setText(R.string.order_status_service);
				viewholder.layout_status
						.setBackgroundResource(R.drawable.icon_dd_label3);
				break;
			case Value.ORDER_STATUS_OFFCAR:// 已下车
				viewholder.layout_status
						.setBackgroundResource(R.drawable.icon_dd_label3);
				viewholder.tv_status.setText(R.string.order_status_pay);
				break;
			case Value.ORDER_STATUS_FINISH:// 已完成
				viewholder.layout_price.setVisibility(View.VISIBLE);
				// 待评价
				if (valuate_status == Value.VALUATE_STATUS_NO) {
					viewholder.layout_status
							.setBackgroundResource(R.drawable.icon_dd_label4);
					viewholder.tv_status
							.setText(R.string.order_status_no_evaluat);
					break;
				} else {
					viewholder.layout_status
							.setBackgroundResource(R.drawable.icon_dd_label4);
					viewholder.tv_status.setText(R.string.order_status_finish);
					break;
				}
			case Value.ORDER_STATUS_CANCEL:// 已取消
			case Value.ORDER_STATUS_KF_CANCEL:// 已取消
			case Value.ORDER_STATUS_GAIPAI_CANCEL:// 已取消
				viewholder.layout_status
						.setBackgroundResource(R.drawable.icon_dd_label4);
				viewholder.tv_status.setText(R.string.order_status_cancel);
				break;
			default:
				viewholder.layout_status
						.setBackgroundResource(R.drawable.icon_dd_label4);
				viewholder.tv_status.setText(R.string.order_status_finish);
				break;
			}
		}

	}
}
